//package com.yt.sys.utils;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import com.yt.sys.models.service.ISysUserService;
//import com.yt.sys.models.service.ISysRoleService;
//import com.yt.sys.models.dto.SysRoleDTO;
//import com.yt.sys.models.dto.SysPermissionDTO;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Component
//public class PermissionCheckUtil {
//
//    @Autowired
//    private ISysUserService sysUserService;
//
//    @Autowired
//    private ISysRoleService sysRoleService;
//
//    /**
//     * 检查用户是否拥有指定权限
//     */
//    public boolean hasPermission(Long userId, String permissionCode) {
//        // 获取用户所有角色
//        List<SysRoleDTO> roles = sysUserService.getUserRoles(userId);
//        if (roles.isEmpty()) {
//            return false;
//        }
//
//        // 检查所有角色是否拥有指定权限
//        for (SysRoleDTO role : roles) {
//            List<SysPermissionDTO> permissions = sysRoleService.getRolePermissions(role.getId());
//            boolean has = permissions.stream()
//                    .anyMatch(p -> permissionCode.equals(p.getPermissionCode()));
//            if (has) {
//                return true;
//            }
//        }
//        return false;
//    }
//}
package com.yt.sys.utils;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yt.sys.models.service.ISysUserService;
import com.yt.sys.models.service.ISysRoleService;
import com.yt.sys.models.dto.SysRoleDTO;
import com.yt.sys.models.dto.SysPermissionDTO;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class PermissionCheckUtil {

    // 超级管理员角色标识（与数据库中一致）
    private static final String SUPER_ADMIN_ROLE = "ADMIN";

    @DubboReference
    private ISysUserService sysUserService;

    @DubboReference
    private ISysRoleService sysRoleService;

    /**
     * 判断用户是否为超级管理员
     */
    public boolean isSuperAdmin(Long userId) {
        List<SysRoleDTO> roles = sysUserService.getUserRoles(userId);
        return roles.stream()
                .anyMatch(role -> SUPER_ADMIN_ROLE.equals(role.getRoleLogo()));
    }

    /**
     * 检查权限（超级管理员默认拥有所有权限）
     */
    public boolean hasPermission(Long userId, String permissionCode) {
        // 超级管理员直接返回有权限
        if (isSuperAdmin(userId)) {
            return true;
        }
        // 普通用户走正常权限校验（原有逻辑）
        List<SysRoleDTO> roles = sysUserService.getUserRoles(userId);
        if (roles.isEmpty()) {
            return false;
        }
        for (SysRoleDTO role : roles) {
            List<SysPermissionDTO> permissions = sysRoleService.getRolePermissions(role.getId());
            boolean has = permissions.stream()
                    .anyMatch(p -> permissionCode.equals(p.getPermissionCode()));
            if (has) {
                return true;
            }
        }
        return false;
    }
}